﻿using System;
using System.Web.Mvc;
using System.Data.Linq;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using OfficeOpenXml;
using OfficeOpenXml.Style;

namespace ASPMVCUtil.ExcelExporter
{
    public class ContentTypesExcel
    {
        public const String CONETNTTYPE1 = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    }

    public class ExcelResult<TModel> : ActionResult
    {
        private string _fileName;
        private IEnumerable<TModel> _rows;
        private IEnumerable<ExcelColumn<TModel>> _columns;

        public string FileName { get { return _fileName; } }
        public IEnumerable Rows { get { return _rows; } }

        public ExcelResult(IEnumerable<TModel> rows, string fileName, IEnumerable<ExcelColumn<TModel>> columns)
        {
            _rows = rows;
            _fileName = fileName;
            _columns = columns;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            using (ExcelPackage pck = new ExcelPackage())
            {
                int numColumnas = _columns.Count();
                //Create the worksheet
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("List");

                int col = 1;
                ws.InsertRow(1, 1);
                foreach (ExcelColumn<TModel> columna in _columns)
                {
                    ws.Cells[1, col].Value = columna.Header;
                    col++;
                }

                ////Format the header
                //using (ExcelRange rng = ws.Cells[1, 1, 1, numColumnas])
                //{
                //    rng.Style.Font.Bold = true;
                //    rng.Style.Fill.PatternType = ExcelFillStyle.Solid;                      //Set Pattern for the background to Solid
                //    rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));  //Set color to dark blue
                //    rng.Style.Font.Color.SetColor(Color.White);
                //}

                int row = 2;
                ws.InsertRow(2, _rows.Count());
                foreach (TModel registro in _rows)
                {
                    col = 1;
                    foreach (ExcelColumn<TModel> column in _columns)
                    {
                        ws.Cells[row, col].Value = column.ObtenerValor(registro);
                        column.SetStyle(ws.Cells[row, col]);
                        col++;
                    }
                    row++;
                }

                ws.Tables.Add(ws.Cells[1, 1, row - 1, _columns.Count()], _fileName);

                //Write it back to the client
                HttpContext httpContext = HttpContext.Current;
                httpContext.Response.Clear();
                httpContext.Response.ContentType = ContentTypesExcel.CONETNTTYPE1;
                httpContext.Response.AddHeader("content-disposition", "attachment;  filename=" + _fileName.Replace(" ", "") + ".xlsx");
                httpContext.Response.BinaryWrite(pck.GetAsByteArray());
            }      
        }
    }
}